package cn.leida.ruoyi.framework.jpa.jpa;

import cn.leida.ruoyi.common.annotation.Excel;
import cn.leida.ruoyi.common.utils.DateUtils;
import cn.leida.ruoyi.common.utils.SecurityUtils;
import jakarta.persistence.Column;
import jakarta.persistence.MappedSuperclass;
import lombok.Data;

import java.io.Serializable;

/**
 * JPA Entity基类
 * MappedSuperclass :  子类表中自动创建属性
 *
 * @author ruoyi
 */
@Data
@MappedSuperclass
public class BaseJPAEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 所有子类具有字段
     */
    @Excel(name = "创建者")
    @Column(name = "create_by", length = 64)
    private String createBy;

    /**
     * yyyyMMdd
     */
    @Excel(name = "创建日期")
    @Column(name = "create_day")
    private Long createDay;

    /**
     * yyyyMMddHHmm
     */
    @Excel(name = "创建时间")
    @Column(name = "create_time")
    private Long createTime;


    @Excel(name = "更新者")
    @Column(name = "update_by", length = 64)
    private String updateBy;

    /**
     * yyyyMMdd
     */
    @Excel(name = "更新日期")
    @Column(name = "update_day")
    private Long updateDay;

    /**
     * yyyyMMddHHmm
     */
    @Excel(name = "更新时间")
    @Column(name = "update_time")
    private Long updateTime;


    @Excel(name = "备注")
    @Column(name = "remark", length = 500)
    private String remark;

    public void initUpdateTime() {
        updateTime = nowTime();
        updateDay = nowTime();
        updateBy = SecurityUtils.getUsername();
    }

    public void initCreateTime() {
        createTime = nowTime();
        createDay = nowTime();
        createBy = SecurityUtils.getUsername();
    }

    public long nowTime() {
        return DateUtils.dateTimeNowLong();
    }

}
